<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 公告管理 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-button type="primary" class="handle-del mr10" @click="addDialog">新增公告</el-button>
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button>
                <el-input v-model="query.keyword" placeholder="输入公告标题" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="title" label="公告标题"></el-table-column>
                <el-table-column prop="type" label="类型">
                    <template slot-scope="scope">
                        {{ scope.row.type == 0 ? '文本' : '图片' }}
                    </template>
                </el-table-column>
                <el-table-column prop="status" label="状态">
                    <template slot-scope="scope">
                        {{ scope.row.status == 0 ? '禁用' : '启用' }}
                    </template>
                </el-table-column>
                <el-table-column prop="create_time" label="发布时间"></el-table-column>
                <el-table-column prop="update_time" label="更新时间"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-row>
                            <el-button @click="edit(scope.row.id, scope.row)" type="success" size="mini">编辑</el-button>
                            <el-button @click="disable(scope.row.id, scope.row.status)" type="warning" size="mini">{{
                                scope.row.status == 0 ? '启用' : '禁用'
                            }}</el-button>
                            <el-button @click="deleteRow(scope.row.id, scope.$index)" type="danger" size="mini">删除</el-button>
                        </el-row>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.page"
                    :page-size="query.rows"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 新增公告弹出框 -->
        <el-dialog title="新增/编辑" :visible.sync="showDialog" width="30%">
            <el-form ref="form" :model="form" label-width="100px">
                <el-form-item label="公告类型">
                    <el-radio-group v-model="form.type">
                        <el-radio :label="0">文本</el-radio>
                        <el-radio :label="1">图片</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="公告图片" v-if="form.type == 1">
                    <el-upload
                        action=""
                        list-type="picture-card"
                        :on-preview="handlePictureCardPreview"
                        :on-remove="handleRemove"
                        :auto-upload="false"
                    >
                        <i class="el-icon-plus"></i>
                        <div slot="tip" class="el-upload__tip">封面建议尺寸：400*400px，png格式图片</div>
                    </el-upload>
                </el-form-item>
                <el-form-item label="公告标题">
                    <el-input v-model="form.title"></el-input>
                </el-form-item>
                <el-form-item label="公告描述">
                    <el-input type="textarea" :rows="2" v-model="form.content"> </el-input>
                </el-form-item>
                <el-form-item label="链接标题">
                    <el-input v-model="form.link_to.title"></el-input>
                </el-form-item>
                <el-form-item label="链接URL">
                    <el-input v-model="form.link_to.param.url"></el-input>
                </el-form-item>
                <el-form-item label="排序">
                    <el-input v-model="form.sort"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-radio-group v-model="form.status">
                        <el-radio :label="1">启用</el-radio>
                        <el-radio :label="0">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { announceListData, addAnnounceData, editAnnounceData, delAnnounceData } from '../../api/index';

export default {
    name: 'announce_manage',
    data() {
        return {
            query: {
                page: 1,
                rows: 4,
                keyword: ''
            },
            form: {
                id: '',
                type: 0,
                title: '',
                content: '',
                link_to: {
                    sys_type: 'out_url',
                    type: 'out_url',
                    title: '',
                    type_title: '自定义链接',
                    param: {
                        title: '自定义链接',
                        url: ''
                    }
                },
                sort: '',
                status: 1
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            showDialog: false,
            pageTotal: 0
        };
    },
    created() {
        this.getData();
    },
    methods: {
        getData() {
            announceListData(this.query).then((res) => {
                this.tableData = res.data.list;
                this.pageTotal = res.data.count;
            });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        //批量删除
        delAllSelection() {
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                const length = this.multipleSelection.length;
                let str = '';
                let strID = '';
                this.delList = this.delList.concat(this.multipleSelection);
                console.log(this.delList);
                for (let i = 0; i < length; i++) {
                    str += this.multipleSelection[i].title + ' ';
                    strID += this.multipleSelection[i].id + ',';
                }
                delAnnounceData({ id: strID.substring(0, strID.lastIndexOf(',')) }).then((res) => {
                    if (res.code == 1) {
                        this.$message.success(`删除了${str}`);
                        this.multipleSelection = [];
                    }
                });
            });
        },
        // 搜索
        handleSearch() {
            this.$set(this.query);
            this.getData();
        },
        // 图片上传
        handleRemove(file, fileList) {
            console.log(file, fileList);
        },
        handlePictureCardPreview(file) {
            this.dialogImageUrl = file.url;
            this.form.dialogVisible = true;
        },
        //新增
        addDialog() {
            this.form = {
                id: '',
                type: 0,
                title: '',
                content: '',
                link_to: {
                    sys_type: 'out_url',
                    type: 'out_url',
                    title: '',
                    type_title: '自定义链接',
                    param: {
                        title: '自定义链接',
                        url: ''
                    }
                },
                sort: '',
                status: 1
            };
            this.showDialog = true;
        },
        //编辑
        edit(id, row) {
            this.form.id = id;
            this.showDialog = true;
            this.form = row;
        },
        //禁用
        disable(id, status) {
            this.$confirm(`确定${status == 0 ? '启用' : '禁用'}吗！`, '提示', {
                type: 'warning'
            }).then(() => {
                editAnnounceData({ id: id, status: status == 0 ? 1 : 0 }).then((res) => {
                    if (res.code == 1) {
                        this.$message.success('成功');
                    }
                });
            });
        },
        //删除
        deleteRow(id, index) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                delAnnounceData({ id: id }).then((res) => {
                    if (res.code == 1) {
                        this.$message.success('删除成功');
                        this.tableData.splice(index, 1);
                    }
                });
            });
        },
        //取消
        cancel() {
            this.showDialog = false;
        },
        //确定
        save() {
            addAnnounceData(this.form).then((res) => {
                if (res.code == 1) {
                    location.reload();
                }
            });
            this.showDialog = false;
        },
        // 分页
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
    float: right;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}

.mr10 {
    margin-right: 10px;
}
</style>


